package cn.edu.cdu.group4nb.controller;

import cn.edu.cdu.group4nb.po.PageInf;
import cn.edu.cdu.group4nb.po.RoleInf;
import cn.edu.cdu.group4nb.po.UserInf;
import cn.edu.cdu.group4nb.service.RoleService;
import cn.edu.cdu.group4nb.service.impl.UserServiceImpl;
import cn.edu.cdu.group4nb.utils.R;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @auther: tupeng
 * @date: 2019-10-10 16:06
 */
@Controller
@RequestMapping("/user")
public class UserController {

    private final UserServiceImpl userService;
    private final RoleService roleService;

    @Autowired
    public UserController(UserServiceImpl userService, RoleService roleService) {
        this.userService = userService;
        this.roleService = roleService;
    }

    @RequestMapping("/getAllUser")
    @ResponseBody
    public List<UserInf> getAllUser() {
        return userService.queryAllUser();
    }

    @RequestMapping("/add")
    @ResponseBody
    public R addUser(@RequestBody Map<String, Object> map) {
        String userid = (String) map.get("userid");
        String password = (String) map.get("password");
        String name = (String) map.get("name");
        String githubURL = (String) map.get("githubURL");
        String email = (String) map.get("email");
        String password1 = new Md5Hash(password, userid, 1024).toHex();
        UserInf userInf = new UserInf();
        userInf.setUserid(userid);
        userInf.setPassword(password1);
        userInf.setGithuburl(githubURL);
        userInf.setEmail(email);
        userInf.setName(name);
        userService.addUser(userInf);
        return R.ok();
    }

    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseBody
    public R roleManagerDelete(@RequestBody int[] user_info_id) {
//        ArrayList<String> list = new ArrayList<String>(Arrays.asList(roleid)) ;
//        System.out.println(user_info_id.toString());
        for (Integer integer : user_info_id) {
            System.out.println(integer);
        }
        try {
            userService.deleteUser(user_info_id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.ok();
    }

    @GetMapping("/getRoleById")
    @ResponseBody
    public List<RoleInf> getRoleById(
//            @RequestBody Map<String, Object> mapr
//            @RequestParam("user_info_id") int user_info_id
            int user_info_id
            ) {
//        String user_info_id= (String) map.get("user_info_id");
//        Integer user_info_id1=Integer.parseInt(user_info_id);
//        System.out.println(user_info_id1);
        //        return R.ok().put("roleInfs", roleInfs);
        List<RoleInf> roleById = userService.getRoleById(user_info_id);
//        for(RoleInf roleInf:roleById){
//            System.out.println(roleInf.getRoleid());
//
//        }
        return roleById;
    }

    @RequestMapping(value = "/addRoleToUser",method = RequestMethod.POST)
    @ResponseBody
    public R addRoleToUser(@RequestBody RoleInf roleInf) {
//        System.out.println(roleInf.getRoleid());
//        System.out.println(roleInf.getUserInfoId());
        userService.addRoleToUser(roleInf);
        return R.ok();
    }

    @RequestMapping(value = "/deleteRoleFromUser",method = RequestMethod.POST)
    @ResponseBody
    public R deleteRole(@RequestBody int[] roleid) {
        userService.deleteRoleFromUser(roleid);
        return R.ok();
    }
    @RequestMapping(value = "/queryRole",method = RequestMethod.GET)
    @ResponseBody
    public R queryRole(){
        return R.ok().put("allRole",roleService.queryAllRole());
    }




    @GetMapping("/getPageById")
    @ResponseBody
    public List<PageInf> getPageById(
//            @RequestBody Map<String, Object> map
//            @RequestParam("user_info_id") int user_info_id
            int user_info_id
    ) {
//        String user_info_id= (String) map.get("user_info_id");
//        Integer user_info_id1=Integer.parseInt(user_info_id);
//        System.out.println(user_info_id1);
        //        return R.ok().put("roleInfs", roleInfs);
        List<PageInf> pageInfs = userService.getPageById(user_info_id);
//        for(RoleInf roleInf:roleById){
//            System.out.println(roleInf.getRoleid());
//
//        }
        return pageInfs;
    }

    @RequestMapping(value = "/addPageToRole",method = RequestMethod.POST)
    @ResponseBody
    public R addPageToRole(@RequestBody PageInf pageInf) {
//        System.out.println(roleInf.getRoleid());
//        System.out.println(roleInf.getUserInfoId());
        userService.addPageToRole(pageInf);
        return R.ok();
    }

    @RequestMapping(value = "/deletePageFromRole",method = RequestMethod.POST)
    @ResponseBody
    public R deletePage(@RequestBody int[] pageid) {
//        for (int id:pageid)

//        System.out.println(id);
        try {
            userService.deletePageFromRole(pageid);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.ok();
    }
    @RequestMapping(value = "/queryPage",method = RequestMethod.GET)
    @ResponseBody
    public R queryPage(){
        return R.ok().put("allPage",userService.queryAllPage());
    }


}
